package com.heima.wemedia.controller.v1;

import com.heima.common.constants.wemedia.WemediaConstants;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.wemedia.dtos.NewsAuthDto;
import com.heima.model.wemedia.dtos.WmNewsDto;
import com.heima.model.wemedia.dtos.WmNewsPageReqDto;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.service.WmNewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Api(value = "自媒体文章API")
@RequestMapping("/api/v1/news")
@RestController
public class WmNewsController {
    @Autowired
    private WmNewsService wmNewsService;

    @ApiOperation("分页带条件查询自媒体文章列表")
    @PostMapping("/list")
    public ResponseResult findNewsByPage(@RequestBody WmNewsPageReqDto dto) {
        return wmNewsService.findNewsByPage(dto);
    }

    @ApiOperation(value = "添加文章信息", notes = "发表文章，修改文章，保存草稿公用的方法")
    @PostMapping("/submit")
    public ResponseResult submit(@RequestBody WmNewsDto wmNewsDto) {
        return wmNewsService.submit(wmNewsDto);
    }

    @ApiOperation(value = "根据id查询文章信息")
    @GetMapping("/one/{id}")
    public ResponseResult findNewById(@PathVariable("id") Integer nid) {
        return wmNewsService.findNewById(nid);
    }

    @ApiOperation(value = "自媒体文章上架或下架", notes = "enable 上架: 1 下架: 0")
    @PostMapping("/down_or_up")
    public ResponseResult downOrUp(@RequestBody WmNewsDto dto) {
        return wmNewsService.newsUpOrDown(dto);
    }

    @ApiOperation(value = "根据文章id删除文章")
    @GetMapping("/del_news/{id}")
    public ResponseResult delNews(@PathVariable("id") Integer id) {
        return wmNewsService.deleteNewById(id);
    }

    @ApiOperation(value = "根据id修改自媒体文章")
    @PutMapping("/update")
    public ResponseResult updateWmNews(@RequestBody WmNews wmNews) {
        wmNewsService.updateById(wmNews);
        return ResponseResult.okResult();
    }

    @ApiOperation(value = "查询待发布文章id集合")
    @GetMapping("/findRelease")
    public ResponseResult findRelease() {
        System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
        return wmNewsService.findWaitingPublish();
    }

    @ApiOperation(value = "根据状态查询所有文章信息")
    @PostMapping("/list_vo")
    public ResponseResult findPersonChargeNews(@RequestBody NewsAuthDto newsAuthDto) {
        return wmNewsService.findPersonChargeNews(newsAuthDto);
    }

    /**
     * 查询文章详情
     *
     * @param id
     * @return
     */
    @GetMapping("/one_vo/{id}")
    public ResponseResult findWmNewsVo(@PathVariable("id") Integer id) {
        return wmNewsService.findOneById(id);
    }

    /**
     * 文章审核成功
     *
     * @param dto
     * @return
     */
    @ApiOperation("人工审核通过 状态:4")
    @PostMapping("/auth_pass")
    public ResponseResult authPass(@RequestBody NewsAuthDto dto) {
        return wmNewsService.updateNewStatus(WemediaConstants.WM_NEWS_AUTH_PASS, dto);
    }

    /**
     * 文章审核失败
     *
     * @param dto
     * @return
     */
    @ApiOperation("人工审核失败 状态:2")
    @PostMapping("/auth_fail")
    public ResponseResult authFail(@RequestBody NewsAuthDto dto) {
        return wmNewsService.updateNewStatus(WemediaConstants.WM_NEWS_AUTH_FAIL, dto);
    }
}
